<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="报障单号" prop="reportId">
        <el-input
          v-model="queryParams.reportId"
          placeholder="请输入报障单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="标题" prop="reportTitle">
        <el-input
          v-model="queryParams.reportTitle"
          placeholder="请输入标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="报障单状态" prop="reportStatus">
        <el-select v-model="queryParams.reportStatus" placeholder="请选择报障单状态" clearable size="small" multiple
                   collapse-tags>
          <el-option
            v-for="dict in dict.type.report_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="报障单处理人" prop="reportDealUserId">
        <el-input
          v-model="queryParams.reportDealUserId"
          placeholder="请输入报障单处理人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['report:ReportOrder:add']"
        >新增
        </el-button>
      </el-col>
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="success"-->
      <!--          plain-->
      <!--          icon="el-icon-edit"-->
      <!--          size="mini"-->
      <!--          :disabled="single"-->
      <!--          @click="handleUpdate"-->
      <!--          v-hasPermi="['report:ReportOrder:edit']"-->
      <!--        >修改-->
      <!--        </el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="danger"-->
      <!--          plain-->
      <!--          icon="el-icon-delete"-->
      <!--          size="mini"-->
      <!--          :disabled="multiple"-->
      <!--          @click="handleDelete"-->
      <!--          v-hasPermi="['report:ReportOrder:remove']"-->
      <!--        >删除-->
      <!--        </el-button>-->
      <!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['report:ReportOrder:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ReportOrderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="报障单号" align="center" prop="reportId" width="200px"/>
      <el-table-column label="故障地点" align="center" prop="reportLocation"/>
      <el-table-column label="标题" align="center" prop="reportTitle"/>
      <el-table-column label="故障大类" align="center" prop="reportTypeBigName"/>
      <el-table-column label="故障小类" align="center" prop="reportTypeSmallName"/>
      <el-table-column label="故障描述" align="center" prop="reportDesc"/>
      <el-table-column label="报障单状态" align="center" prop="reportStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.report_status" :value="scope.row.reportStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="处理人" align="center" prop="reportDealUserName"/>
      <el-table-column label="操作" align="left" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['report:ReportOrder:edit']"
            v-if="scope.row.reportStatus == 'EDIT'"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleSubmit(scope.row)"
            v-if="scope.row.reportStatus == 'EDIT'"
          >提交
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleBack(scope.row)"
            v-if="scope.row.reportStatus == 'CREATE'"
          >撤回
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
            v-if="scope.row.reportStatus == 'EDIT'"
          >作废
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleReopen(scope.row)"
            v-if="scope.row.reportStatus == 'COMPLETE'"
          >重新打开
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleStar(scope.row)"
            v-if="scope.row.reportStatus == 'COMPLETE'"
          >评价
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="历史记录" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-more"
            @click="handleHistory(scope.row)"
          >历史
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <report-time-line :history-drawer-open.sync="historyDrawerOpen" :time-line-item-list="timeLineItemList"/>

    <el-dialog :visible.sync="starDialogOpen" width="300px" title="报障单评价" :close-on-click-modal="false">
      <el-form :model="starForm" :rules="starFormRule" ref="reportStarForm">
        <el-form-item label="评分">
          <el-rate
            style="margin-top: 9px"
            v-model="starForm.starValue"
            :max="5"
            :value="5"
            allow-half
            show-score>
          </el-rate>
        </el-form-item>
        <el-form-item label="评价" prop="starInfo">
          <el-input maxlength="100" show-word-limit placeholder="请输入评价" type="textarea" v-model="starForm.starInfo">
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitStarForm">提交</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listReportOrder,
  getReportOrder,
  delReportOrder,
  addReportOrder,
  updateReportOrder, reopenReportOrder, starReportOrder
} from "@/api/report/ReportOrder";
import {parseTime} from "@/utils/ruoyi";
import ReportOrderComp from "./addReportOrder"
import request from "@/utils/request";
import ReportTimeLine from "@/views/report/comp/reportTimeLine";
import {resetUserPwd} from "@/api/system/user";

export default {
  name: "ReportOrder",
  dicts: ['report_status'],
  components: {
    ReportTimeLine,
    ReportOrderComp
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 报障单表格数据
      ReportOrderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        reportTitle: null,
        reportStatus: ["CREATE", "DEALING", "COMPLETE", "BACKING", "STARED", "EDIT"],
        reportDealUserId: null,
      },
      historyDrawerOpen: false,
      timeLineItemList: [],
      starDialogOpen: false,
      starForm: {
        starValue: 5,
        starInfo: ""
      },
      starFormRule: {
        starInfo: [
          {validator: this.checkStarInfo, tigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    checkStarInfo(rule, value, callback, source) {
      if (this.starForm.starValue < 4 && !value) {
        callback(new Error("请输入评价内容"))
      }else{
        callback()
      }
    },
    /** 查询报障单列表 */
    getList() {
      this.loading = true;
      listReportOrder(this.queryParams).then(response => {
        this.ReportOrderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        reportId: null,
        reportUserId: null,
        reportLocation: null,
        reportTitle: null,
        reportProduct: null,
        reportTimeStart: null,
        reportTimeEnd: null,
        reportTypeBig: null,
        reportTypeBigName: null,
        reportTypeSmall: null,
        reportTypeSmallName: null,
        reportDesc: null,
        reportFiles: null,
        reportStar: null,
        reportStarInfo: null,
        reportStarTime: null,
        phone: null,
        email: null,
        qq: null,
        reportStatus: null,
        reportDealCount: null,
        reportDealUserId: null,
        reportSolution: null,
        reportReason: null,
        createBy: null,
        createTime: null,
        userId: null,
        deptId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      //跳转
      this.$router.push({path: "/report/reportUpload"});
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const id = row.id || this.ids
      this.$router.push({path: "/report/reportUpload?id=" + id});
    },
    handleHistory(row) {
      const id = row.id
      request({
        url: '/report/ReportOrder/reportHistory/' + id,
        method: 'get'
      }).then(res => {
        this.timeLineItemList = res.data
      })
      //查询历史，右侧显示流程
      this.historyDrawerOpen = true
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateReportOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addReportOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    handleSubmit(row) {
      var that = this
      this.$modal.confirm('是否确认提交该报障单？').then(function () {
        const id = row.id
        request({
          url: '/report/ReportOrder/submit/' + id,
          method: 'post'
        }).then(res => {
          that.getList()
        })
      })

    },
    handleBack(row) {
      var that = this
      this.$modal.confirm('是否确认撤回该报障单？').then(function () {
        const id = row.id
        request({
          url: '/report/ReportOrder/back/' + id,
          method: 'post'
        }).then(res => {
          that.getList()
        })
      })

    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认作废报障单？').then(function () {
        return delReportOrder(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 重新打开操作 */
    handleReopen(row) {
      const ids = row.id || this.ids;
      this.$prompt('请输入重新打开的原因', "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        closeOnClickModal: false,
        inputPattern: /^.{5,100}$/,
        inputErrorMessage: "原因需要在5-100个字符之间",
      }).then(({value}) => {
        this.loading = true
        reopenReportOrder({
          id: ids,
          reOpenInfo: value
        }).then(res => {
          this.loading = false
          this.$message.success("重新打开成功")
          this.getList()
        })
      }).catch(() => {
      });
    },
    /** 评价操作 */
    handleStar(row) {
      const ids = row.id || this.ids;
      this.starDialogOpen = true
      this.starForm = {
        starValue: 5,
        starInfo: "",
        id: ids
      }
    },
    submitStarForm() {
      this.$refs.reportStarForm.validate(valid => {
        if (valid){
          //如果评分小于4，则进行弹窗
          if (this.starForm.starValue < 4) {
            this.$confirm('确定评价吗？', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(res => {
              starReportOrder(this.starForm).then(res => {
                this.$message.success("评价成功")
                this.starDialogOpen = false
                this.getList()
              })
            }).catch()
          } else {
            starReportOrder(this.starForm).then(res => {
              this.$message.success("评价成功")
              this.starDialogOpen = false
              this.getList()
            })
          }
        }
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('report/ReportOrder/export', {
        ...this.queryParams
      }, `ReportOrder_${new Date().getTime()}.xlsx`)
    }
  },
  activated() {
    let refresh = this.$route.query.refresh
    if (refresh == "true") {
      this.getList()
    }
  },
};
</script>
